#include "chatframe.h"

ChatFrame::ChatFrame(const QString &sender, const QString &c, const UserType& mt, const NewsType &nt, QWidget *parent)
    : QWidget{parent}
    , userType(mt)
    ,nt(nt)
    ,content(c)
    ,sender(sender)     ,green(149,236,105),white(255,255,255)
{
    pixmap.load("./data/head portrait/" + sender + "_HeadPortrait.jpg");
    if (sender == "与讯飞星火的聊天"){
        pixmap.load(":/headPortrait/img/head portrait/xunfei.png");
    }

    if (nt == NewsType::text){
        initText();
    }else{
        initFile();
    }

    //来减少重绘次数
    setAutoFillBackground(false);
}

ChatFrame::~ChatFrame()
{
    delete label;
}

QSize ChatFrame::sizeHint() const {
    QSize size = label->sizeHint();
    size.setWidth(size.width() + 20);
    size.setHeight(size.height() + 20);
    return size;
}

void ChatFrame::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing, true);
    QPen pen(white);
    QBrush brush(white);
    int x = 0;

    //设置当前用户气泡框的显示样式
    if (userType == UserType::Current_User)
    {
        pen.setColor(green);
        brush.setColor(green);

        int width = painter.device()->width();
        x = width - 34;
        points[0].setX(width - LOCATION_X + 5);
        for (int i = 1; i < 3; i++){
            points[i].setX(width - LOCATION_X);
        }
        label->move(this->width() - label->width() - LOCATION_X, LOCATION_Y - 10);//位置
    }
    painter.setPen(pen);
    painter.setBrush(brush);
    //绘制头像
    painter.drawPixmap(x, 10, 34, 34, pixmap);
    //多边形
    if (nt == NewsType::text) painter.drawConvexPolygon(points, 3);

    //其他用户绘制名字
    if (userType == UserType::Other_Users){
        QFont font("华文细黑", 10);
        font.setWeight(QFont::Light); // 设置为轻量级，这通常类似于细体

        pen.setColor(QColor(150,150,150));
        painter.setPen(pen);
        painter.setFont(font);
        painter.drawText(QRect(LOCATION_X - 5, 5, 200, 30), Qt::TextWordWrap | Qt::AlignLeft, sender);
    }
}

void ChatFrame::resizeEvent(QResizeEvent *event)
{
    if (nt != NewsType::text){
        return;
    }
    QSize textRect = label->sizeHint();
    emit InterfaceUpdate(textRect);
    if ((this->width() / 2 + 30) > textAllWidth){
        label->setFixedWidth(textAllWidth);
        label->setFixedHeight(textRect.height() - 30);
    }else{
        label->setFixedWidth(this->width() / 2 + 30);
        label->setFixedHeight(textRect.height() - 30);
    }
}

void ChatFrame::initText()
{
    points[0] = QPointF(LOCATION_X - 5, VERTEX_TRIANGLE);
    points[1] =QPointF(LOCATION_X, VERTEX_TRIANGLE - 5);
    points[2] = QPointF(LOCATION_X, VERTEX_TRIANGLE+ 5);

    QFont font("华文细黑", 12);
    font.setWeight(QFont::Light); // 设置为轻量级，这通常类似于细体
    this->setFont(font);
    QFontMetrics metrics(this->font());
    textAllWidth = metrics.horizontalAdvance(content) + 20;

    label = new MyLabel(static_cast<bool>(userType), content, this);
    label->move(LOCATION_X, LOCATION_Y);//位置
}

void ChatFrame::initFile()
{
    label = new FileMessage(content, nt, this);
    label->move(LOCATION_X, LOCATION_Y);//位置
}
